﻿using UnityEngine;

namespace Core
{
    public static partial class Extensions
    {
        /// <summary>
        /// 判断是否在[zero,range)范围内
        /// </summary>
        /// <param name="v"></param>
        /// <param name="range"></param>
        /// <returns></returns>
        public static bool Within(this Vector2Int v, Vector2Int range)
        {
            return v is { x: >= 0, y: >= 0 } && v.x < range.x && v.y < range.y;
        }

        /// <summary>
        /// 判断是否在[min,max)范围内
        /// </summary>
        /// <param name="v"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static bool Within(this Vector2Int v, Vector2Int min, Vector2Int max)
        {
            return v.x >= min.x && v.y >= min.y && v.x < max.x && v.y < max.y;
        }

        public static bool Within(this Vector2Int v, Region region)
        {
            return v.Within(region.Coord, region.Coord + region.Size);
        }

        public static bool Within(this Region region, Vector2Int range)
        {
            var coord = region.Coord;
            var size = region.Size;

            return coord.Within(range) &&
                   coord.Plus(size.x - 1, 0).Within(range) &&
                   coord.Plus(0, size.y - 1).Within(range) &&
                   coord.Plus(size.x - 1, size.y - 1).Within(range);
        }

        /// <summary>
        /// 判断是否在边界上
        /// </summary>
        /// <param name="v"></param>
        /// <param name="range"></param>
        /// <returns></returns>
        public static bool OnBorder(this Vector2Int v, Vector2Int range)
        {
            return v.x == 0 || v.y == 0 || v.x == range.x - 1 || v.y == range.y - 1;
        }

        public static Vector2Int Plus(this Vector2Int v, int i, int j)
        {
            return v + new Vector2Int(i, j);
        }

        // public static Vector2 Plus(this Vector2Int v, float i, float j)
        // {
        //     return v + new Vector2(i, j);
        // }
        //
        // public static Vector2Int Minus(this Vector2Int v, int i, int j)
        // {
        //     return v - new Vector2Int(i, j);
        // }
        //
        // public static Vector2 Minus(this Vector2Int v, float i, float j)
        // {
        //     return v - new Vector2(i, j);
        // }

        public static Vector2 ToPos(this Vector2Int v)
        {
            return v + Vector2.one * 0.5f;
        }

        public static Vector2 ToRandPos(this Vector2Int v)
        {
            var x = Random.Range(0f, 1f);
            var y = Random.Range(0f, 1f);
            return v + new Vector2(x, y);
        }

        public static Vector2 ToRandPos(this Vector2 v, float radius)
        {
            var x = Random.Range(0f, radius);
            var y = Random.Range(0f, radius);
            return v + new Vector2(x, y);
        }

        public static Vector2Int ToCoord(this Vector2 v)
        {
            return Utils.Math.GetInt(v);
        }
    }
}
